﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using hlavny.modul1;

namespace hlavny.modul2
{
    public partial class CreateColumnForm : Form
    {
        public CreateColumnForm()
        {
            InitializeComponent();
            labelError.Visible = false;
            textBoxColumnLength.Enabled = true;

            comboBoxColumnType.DataSource = null;
            comboBoxColumnType.Items.Clear();
            comboBoxColumnType.DataSource = CTypes.EnumTypes.GetNames(typeof(CTypes.EnumTypes));

            comboBoxColumnNullability.Items.Clear();
            comboBoxColumnNullability.Items.Add("NOT NULL");
            comboBoxColumnNullability.Items.Add("NULL");
            comboBoxColumnNullability.SelectedIndex = 0;
        }

        private void ErrorMessage(string message)
        {
            buttonCreateColumn.Location = new Point(92, 135);
            labelError.Visible = true;
            labelError.Text = message;
            textBoxColumnName.Focus();
            textBoxColumnName.SelectAll();
        }

        private void buttonCreateColumn_Click(object sender, EventArgs e)
        {
            if (CFunctions.IsValidName(textBoxColumnName.Text))
            {
                if (!CDBS.SysTables.CurrentTable.ContaintColumnName(textBoxColumnName.Text, CDBS.SysColumns))
                {
                    CTypes.EnumTypes type = (CTypes.EnumTypes)Enum.Parse(typeof(CTypes.EnumTypes), comboBoxColumnType.SelectedValue.ToString()); //takto sa da parsovat zvoleny typ
                    int length = 0;
                    if (textBoxColumnLength.Text != "")
                    {
                        try
                        {
                            length = Int32.Parse(textBoxColumnLength.Text);
                        }
                        catch
                        {
                            length = 0;
                        }
                    }
                    bool nullability;
                    if (comboBoxColumnNullability.SelectedIndex == 0) 
                    {
                        nullability = false;
                    }
                    else 
                    {
                        nullability = true;
                    }
                    CDBS.SysTables.AddAttr(CDBS.SysColumns.CreateAtr(textBoxColumnName.Text, type, length, nullability, CDBS.SysTables.CurrentTable.Id));
                    this.DialogResult = DialogResult.OK;
                }
                else
                {
                    ErrorMessage("Stĺpec so zadaným názvom uz existuje!");
                }
            }
            else
            {
                ErrorMessage("Zadali ste neplatný názov!");
            }
        }

        private void comboBoxColumnType_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (comboBoxColumnType.SelectedValue.ToString() == "Character")
            {
                textBoxColumnLength.Enabled = true;
            }
            else
            {
                textBoxColumnLength.Enabled = false;
            }
        }


    }
}
